package com.xy.p2p.dao.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.sun.org.apache.bcel.internal.generic.NEW;
import com.xy.common.page.Pagination;
import com.xy.core.dao.impl.BaseDaoImpl;
import com.xy.p2p.dao.TfinanceinfoDao;
import com.xy.p2p.model.Tfinanceinfo;

public class TfinanceinfoDaoImpl extends BaseDaoImpl<Tfinanceinfo> implements TfinanceinfoDao {
	public TfinanceinfoDaoImpl() {
		super(Tfinanceinfo.class);
	}
   
   
	@Override
	public List<Tfinanceinfo> getTfinanceinfo(Map<String, Object> map) {
		Object[] values = map.values().toArray();
		List<Tfinanceinfo> localList = findByHql(getHql(map)+" order by applicationdate desc", values);
		return localList;
	}

	@Override
	public Pagination getTfinanceinfoByPage(Map<String, Object> map, int pageNo,
			int pageSize) {
		Object[] values = map.values().toArray();
		return findByPage(getHql(map)+" order by applicationdate desc", values, pageNo, pageSize);
	}

	private String getHql(Map<String, Object> map) {
		StringBuffer str = new StringBuffer("from Tfinanceinfo tt");
		if(map.size()>0)str.append(" where ");
		Set<String> set = map.keySet();
		StringBuffer where = new StringBuffer();
		for (String name : set) {
			if (where.length() > 0)
				where.append(" and  ");
			where.append("tt.").append(name).append("=?");
		}
		return str.append(where.toString()).toString();
	}


	@Override
	public List<Tfinanceinfo> getEnableFinanceInfo() {
		SimpleDateFormat dateformat=new SimpleDateFormat("yyyy-MM-dd");
		String str = "from Tfinanceinfo tt where tt.targettype != 8 and tt.financelimit > '"+dateformat.format(new Date())+"' and tt.status = 2 order by tt.applicationdate";
		return findByHql(str);
	}


	@Override
	public List<Tfinanceinfo> getFullFinanceInfo() {
		SimpleDateFormat dateformat=new SimpleDateFormat("yyyy-MM-dd");
		String str = "from Tfinanceinfo tt where  tt.targettype != 8 and (tt.financelimit > '"+dateformat.format(new Date())+"' and tt.status = 4) or tt.status = 7 order by tt.status,tt.applicationdate desc";
		return findByHql(str);
	}


	@Override
	public Pagination getFinanceInfoByCondition(String[] targetType, List<Map<String, Integer>> dateList, String[] credit, final int pageNo, final int pageSize) {
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
		StringBuffer hql = new StringBuffer(
				"from Tfinanceinfo tt inner join fetch tt.Tmember.Tmembercredits tc where  tt.targettype != 8 and(  (tt.financelimit > '"+dateformat.format(new Date())+"' and tt.status = 2) or tt.status = 4 or tt.status = 7 )");
		StringBuffer c1 = new StringBuffer();
		StringBuffer c2 = new StringBuffer();
		StringBuffer c3 = new StringBuffer();
		List<Object> condition = new ArrayList<Object>();
//		condition.add(new Date());
		// condition targetType
		for (String s : targetType) {
			if (c1.length() == 0) {
				c1.append(" and ( tt.targettype = ? ");
				condition.add(Integer.valueOf(s));
			} else {
				c1.append(" or tt.targettype = ? ");
				condition.add(Integer.valueOf(s));
			}
		}
		if (c1.length() != 0) {
			c1.append(" ) ");
		}
		// condition date
		for (Map<String, Integer> m : dateList) {
			if (c2.length() == 0) {
				c2.append(" and ((tt.financeallotedtime> ? and tt.financeallotedtime<= ? ) ");
				condition.add(m.get("begin"));
				condition.add(m.get("end"));
			} else {
				c2.append(" or (tt.financeallotedtime> ? and tt.financeallotedtime<= ? ) ");
				condition.add(m.get("begin"));
				condition.add(m.get("end"));
			}
		}
		if (c2.length() != 0)
			c2.append(" ) ");
		// condition credit
		for (String s : credit) {
			if (c3.length() == 0) {
				c3.append(" and (  tc.creditrating = ? ");
				condition.add(s);
			} else {
				c3.append(" or tc.creditrating = ? ");
				condition.add(s);
			}
		}
		if (c3.length() != 0)
			c3.append(" ) ");

		hql.append(c1.toString() + c2.toString() + c3.toString() + "order by tt.status,tt.applicationdate desc");
		System.out.println("+++++++++++++++++++++EXECUTE HQL:" + hql.toString());
		return findByPage(hql.toString(), condition.toArray(), pageNo, pageSize);
		// test
		// String hql =
		// "from Tfinanceinfo tt inner join fetch tt.Tmember.Tmembercredits tc where tc.creditrating = 'A' order by tt.status,tt.applicationdate desc";
		// List<Tfinanceinfo> list = findByHql(hql.toString());
	}


}